package org.zjx.operation;


/**
 * 条件执行操作
 */
public class ConditionalOperation implements WindowOperation<Boolean> {
    private final WindowOperation<Boolean> condition;
    private final WindowOperation<?> trueAction;
    private final WindowOperation<?> falseAction;

    public ConditionalOperation(WindowOperation<Boolean> condition,
                                WindowOperation<?> trueAction) {
        this(condition, trueAction, null);
    }

    public ConditionalOperation(WindowOperation<Boolean> condition,
                                WindowOperation<?> trueAction,
                                WindowOperation<?> falseAction) {
        this.condition = condition;
        this.trueAction = trueAction;
        this.falseAction = falseAction;
    }

    @Override
    public Boolean execute(OperationExecutor executor) {
        Boolean result = condition.execute(executor);
        if (Boolean.TRUE.equals(result)) {
            if (trueAction != null) {
                trueAction.execute(executor);
            }
        } else {
            if (falseAction != null) {
                falseAction.execute(executor);
            }
        }
        return result;
    }
}
